package com.obelisco.vista.componentes;

import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Font;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.ResourceImageReference;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.layout.ColumnLayoutData;
import nextapp.echo2.app.layout.GridLayoutData;

import com.obelisco.vista.controles.BindingListBox;

public class DobleLista extends Grid {

	//private ResourceBundle resourceBundle;

	private BindingListBox lstOrigen;
	private List datosOrigen;
	
	private BindingListBox lstDestino;
	private List datosDestino;
	
	private Label lblOrigen;
	private Label lblDestino;
	
	//private String textoOrigen;
	//private String textoDestino;
	
	Integer anchoListaOrigen = 200;
	Integer anchoListaDestino = 200;
	
	Integer altoListaOrigen = 150;
	Integer altoListaDestino = 150;	

	/**
	 * Creates a new <code>DobleLista</code>.
	 */
	public DobleLista() {
		super();

		// Add design-time configured components.
		initComponents();
	}

	public void setLabelOrigen(String textoOrigen) {
		//this.textoOrigen = textoOrigen;
		lblOrigen.setText(textoOrigen);
	}

	public void setLabelDestino(String textoDestino) {
		//this.textoDestino = textoDestino;
		lblDestino.setText(textoDestino);
	}
	
	public void setDatosOrigen(List datosOrigen) {
		this.datosOrigen = datosOrigen;
		lstOrigen.setItems(datosOrigen);
	}
	
	public void setDatosDestino(List datosDestino) {
		this.datosDestino = datosDestino;
		lstDestino.setItems(datosDestino);
	}
	
	private void agregar(ActionEvent e) {
		pasarSeleccion(lstOrigen,lstDestino,datosOrigen,datosDestino);
	}

	
	private void quitar(ActionEvent e) {
		pasarSeleccion(lstDestino,lstOrigen,datosDestino,datosOrigen);
	}

	private void pasarSeleccion(BindingListBox componenteOrigen, BindingListBox componenteDestino, List origen, List destino )
	{				
		if ((origen.size() > 0) && (componenteOrigen.getSelectedValue() != null))
		{			
						
			int[] arregloSeleccionados = componenteOrigen.getSelectedIndices();
			Object objetosRemovidos = origen.remove(arregloSeleccionados[0]);
			destino.add(objetosRemovidos);
			
			componenteOrigen.setItems(origen);
			componenteDestino.setItems(destino);

		}
	}



	/**
	 * Configures initial state of component.
	 * WARNING: AUTO-GENERATED METHOD.
	 * Contents will be overwritten.
	 */
	private void initComponents() {
		this.setInsets(new Insets(new Extent(5, Extent.PX)));
		this.setHeight(new Extent(80, Extent.PERCENT));
		this.setWidth(new Extent(80, Extent.PERCENT));
		//Grid grid1 = new Grid();
		//grid1.setSize(3);
		this.setSize(3);
		//grid1.setHeight(new Extent(100, Extent.PERCENT));
		//add(grid1);
		
		ColumnLayoutData labelLayoutData = new ColumnLayoutData();
		labelLayoutData.setAlignment(new Alignment(Alignment.CENTER, Alignment.CENTER));
		
		Column column1 = new Column();
		GridLayoutData column1LayoutData = new GridLayoutData();
		column1LayoutData.setInsets(new Insets(new Extent(5, Extent.PX)));
		column1.setLayoutData(column1LayoutData);
		//grid1.add(column1);
		this.add(column1);
		lblOrigen = new Label();
		lblOrigen.setText("Origen");
		lblOrigen.setLayoutData(labelLayoutData);
		lblOrigen.setFont(new Font(null, Font.BOLD, new Extent(10, Extent.PT)));
		column1.add(lblOrigen);
		lstOrigen = new BindingListBox(220);
		lstOrigen.setWidth(new Extent(anchoListaOrigen, Extent.PX));
		lstOrigen.setHeight(new Extent(altoListaOrigen, Extent.PX));
		lstOrigen.setStyleName("Obelisco.ListBox");
		column1.add(lstOrigen);
		Column column2 = new Column();
		GridLayoutData column2LayoutData = new GridLayoutData();
		column2LayoutData.setInsets(new Insets(new Extent(10, Extent.PX), new Extent(0, Extent.PX)));
		column2LayoutData.setAlignment(new Alignment(Alignment.CENTER, Alignment.CENTER));
		column2.setLayoutData(column2LayoutData);
		//grid1.add(column2);
		this.add(column2);
		Button button1 = new Button();
		button1.setInsets(new Insets(new Extent(0, Extent.PX), new Extent(5, Extent.PX)));
		ResourceImageReference imageReference1 = new ResourceImageReference("/com/obelisco/vista/resource/image/icon/buttons/adelante.png");
		button1.setIcon(imageReference1);
		ColumnLayoutData button1LayoutData = new ColumnLayoutData();
		button1LayoutData.setAlignment(new Alignment(Alignment.CENTER,Alignment.DEFAULT));
		button1.setLayoutData(button1LayoutData);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				agregar(e);
			}
		});
		column2.add(button1);
		Button button2 = new Button();
		button2.setInsets(new Insets(new Extent(0, Extent.PX), new Extent(5,Extent.PX)));
		ResourceImageReference imageReference2 = new ResourceImageReference("/com/obelisco/vista/resource/image/icon/buttons/atras.png");
		button2.setIcon(imageReference2);
		ColumnLayoutData button2LayoutData = new ColumnLayoutData();
		button2LayoutData.setAlignment(new Alignment(Alignment.CENTER,Alignment.DEFAULT));
		button2.setLayoutData(button2LayoutData);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quitar(e);
			}
		});
		column2.add(button2);
		Column column3 = new Column();
		GridLayoutData column3LayoutData = new GridLayoutData();
		column3LayoutData.setInsets(new Insets(new Extent(5, Extent.PX)));
		column3.setLayoutData(column3LayoutData);
		//grid1.add(column3);
		this.add(column3);
		lblDestino = new Label();
		lblDestino.setText("Destino");
		lblDestino.setLayoutData(labelLayoutData);
		lblDestino.setFont(new Font(null, Font.BOLD, new Extent(10, Extent.PT)));
		column3.add(lblDestino);
		lstDestino = new BindingListBox(220);
		lstDestino.setWidth(new Extent(anchoListaDestino, Extent.PX));
		lstDestino.setHeight(new Extent(altoListaDestino, Extent.PX));
		lstDestino.setStyleName("Obelisco.ListBox");
		column3.add(lstDestino);
	}

	public Collection getDatosDestino() {
		return datosDestino;
	}

	public Collection getDatosOrigen() {
		return datosOrigen;
	}

	public Integer getAltoListaDestino() {
		return altoListaDestino;
	}

	public void setAltoListaDestino(Integer altoListaDestino) {
		lstDestino.setHeight(new Extent(altoListaDestino, Extent.PX));
		this.altoListaDestino = altoListaDestino;
	}

	public Integer getAltoListaOrigen() {
		return altoListaOrigen;
	}

	public void setAltoListaOrigen(Integer altoListaOrigen) {
		lstOrigen.setHeight(new Extent(altoListaOrigen, Extent.PX));
		this.altoListaOrigen = altoListaOrigen;
	}

	public Integer getAnchoListaDestino() {
		return anchoListaDestino;
	}

	public void setAnchoListaDestino(Integer anchoListaDestino) {
		lstDestino.setWidth(new Extent(anchoListaDestino, Extent.PX));
		this.anchoListaDestino = anchoListaDestino;
	}

	public Integer getAnchoListaOrigen() {
		return anchoListaOrigen;
	}

	public void setAnchoListaOrigen(Integer anchoListaOrigen) {
		lstOrigen.setWidth(new Extent(anchoListaOrigen, Extent.PX));
		this.anchoListaOrigen = anchoListaOrigen;
	}
}
